package com.daiziru.wuyanmall.service.impl;

import com.daiziru.wuyanmall.dao.ProductImgMapper;
import com.daiziru.wuyanmall.dao.ProductMapper;
import com.daiziru.wuyanmall.dao.ProductParamsMapper;
import com.daiziru.wuyanmall.dao.ProductSkuMapper;
import com.daiziru.wuyanmall.entity.*;
import com.daiziru.wuyanmall.service.ProductService;
import com.daiziru.wuyanmall.utils.PageHelper;
import com.daiziru.wuyanmall.vo.ResultStatus;
import com.daiziru.wuyanmall.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductParamsMapper productParamsMapper;

    @Autowired
    private ProductService productService;


    @Override
    public ResultVO listRecommendProducts() {
        List<ProductVO> productVOS = productMapper.selectRecommendProducts();
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", productVOS);
        return resultVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO getProductBasicInfo(String productId) {
        //1、商品的基本信息
        Example example = new Example(Product.class);//注意，这个Example的依赖必须是tkmapper的
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);//设置查询的条件
        criteria.andEqualTo("productStatus", 1);

        List<Product> products = productMapper.selectByExample(example);//根据上面设置的两个条件查询

        if(products.size() > 0) {
            //2、查询商品图片

            Example example1 = new Example(ProductImg.class);//注意，这个Example的依赖必须是tkmapper的
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("itemId", productId);//设置查询的条件

            List<ProductImg> productImgs = productImgMapper.selectByExample(example1);

            //3、查询商品套餐
            Example example2 = new Example(ProductSku.class);
            Example.Criteria criteria2 = example2.createCriteria();
            criteria2.andEqualTo("productId", productId);
            criteria2.andEqualTo("status", 1);

            List<ProductSku> productSkus = productSkuMapper.selectByExample(example2);

            HashMap<String, Object> basicInfo = new HashMap<>();
            basicInfo.put("product", products.get(0));
            basicInfo.put("productImgs", productImgs);
            basicInfo.put("productSkus", productSkus);

            return new ResultVO(ResultStatus.OK, "SUCCESS", basicInfo);


        } else {
            return new ResultVO(ResultStatus.NO, "查询的商品不存在", null);
        }

    }

    /**
     * 商品详情的接口实现---对应表格productParams
     * @param productId
     * @return
     */
    @Override
    public ResultVO getProductParamsByPid(String productId) {
        Example example = new Example(ProductParams.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("productId", productId);
        List<ProductParams> productParams = productParamsMapper.selectByExample(example);
        if(productParams.size() > 0) {
            return new ResultVO(ResultStatus.OK, "SUCCESS", productParams.get(0));
        } else {
            return new ResultVO(ResultStatus.NO, "该商品是三无产品", null);
        }

    }


    /**
     * 分类id查询分类商品，并执行分页
     * @param categoryId
     * @param pageNum
     * @param limit
     * @return
     */
    @Override
    public ResultVO getProductsByCategoryId(int categoryId, int pageNum, int limit) {

        //1、查询分页数据
        int start = (pageNum - 1) * limit;
        List<ProductVO> productVOS = productMapper.selectProductByCategoryId(categoryId, start, limit);
        //2、查询总记录数
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId",categoryId);
        int count = productMapper.selectCountByExample(example); //总记录数
        //3、计算总页数
        int pageCount = count % limit == 0 ? (count / limit) : (count / limit + 1);
        //4、封装数据，返回结果
        PageHelper<ProductVO> pageHelper = new PageHelper<>(count, pageCount, productVOS);
        return new ResultVO(ResultStatus.OK, "success", pageHelper);

    }

    //根据类别id查询商品品牌
    @Override
    public ResultVO listBrands(int categoryId) {
        List<String> brands = productMapper.selectBrandsByCategoryId(categoryId);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", brands);
        return resultVO;
    }

    ////根据关键字查询商品品牌
    @Override
    public ResultVO listBrands(String keyword) {
        keyword = "%" + keyword + "%";
        /**
         * 注意，上面的对关键字进行处理，其实模糊查询可以有两种方法
         * （1）一种是使用'%${keyword}%'的形式，这种形式keyword不需要提前处理，就是将模糊处理放在sql语句中
         * （2）另外一种是'#{keyword}'这种形式，这种形式需要在实现类中对keyword进行处理，也就是        keyword = "%" + keyword + "%";
         *需要注意的是，第一种有可能引起sql语句的注入问题，而第二种则避免了这种语句的问题
         */
        List<String> brands = productMapper.selectBrandsByKeyword(keyword);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", brands);
        System.out.println(brands);
        return resultVO;

    }


    @Override
    public ResultVO getProductByBrand(int categoryId, String brand) {
        //1、根据cid查询所有商品，不分页
        List<ProductVO> productVOS = productMapper.selectlistByCid(categoryId);

        //2、遍历每一个商品的brand,符合的加入到数组中
        List<ProductVO> res = new ArrayList<>() ;
        for (ProductVO p: productVOS) {
            String productId = p.getProductId();
            Example example = new Example(ProductParams.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId",productId);
            List<ProductParams> productParams = productParamsMapper.selectByExample(example);
            String brand1 = productParams.get(0).getBrand();
            System.out.println(brand1);
            if(brand.equals(brand1)){
                res.add(p);
            }

        }
        System.out.println("res=====>" + res);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", res);
        return resultVO;
    }

    @Override
    public ResultVO getProductByBrand(String keyword, String brand) {
        //1、根据keyword关键字查询所有商品，不分页
        keyword = "%" + keyword + "%";
        List<ProductVO> productVOS = productMapper.selectProductByKeyWord(keyword, 1, 1000);
        System.out.println("==========================================");
        System.out.println("根据关键字搜索:"+ productVOS);
        //2、遍历每一个商品的brand,符合的加入到数组中
        List<ProductVO> res = new ArrayList<>() ;
        for (ProductVO p: productVOS) {
            String productId = p.getProductId();
            //根据商品id查询商品，然后比较该商品的品牌是否是点击的类型的商品的品牌
            Example example = new Example(ProductParams.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("productId",productId);
            List<ProductParams> productParams = productParamsMapper.selectByExample(example);
            String brand1 = productParams.get(0).getBrand();
            System.out.println(brand1);
            if(brand.equals(brand1)){
                res.add(p);
            }
        }
        System.out.println("res=====>" + res);
        ResultVO resultVO = new ResultVO(ResultStatus.OK, "success", res);
        return resultVO;
    }

    /**根据关键字查询商品，并进行分页**/
    @Override
    public ResultVO selectProduct(String keyword, int pageNum, int limit) {
        //1、查询搜索结果
        keyword = "%" + keyword + "%";
        int start = (pageNum - 1) * limit; //每一次查询的起始位置
        List<ProductVO> productVOS = productMapper.selectProductByKeyWord(keyword, start, limit);
        //2、查询总记录数
        Example example = new Example(Product.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("productName", keyword);
        int count = productMapper.selectCountByExample(example);
        //3、计算总页数
        int pageCount = count % limit == 0 ? count / limit : count / limit + 1;
        //4、封装返回数据
        PageHelper<ProductVO> pageHelper = new PageHelper<>(count, pageCount, productVOS);
        return new ResultVO(ResultStatus.OK,"success", pageHelper);
    }


}
